home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
MAXMEM__
/
MAXAPPLZ.C1
< prev
Wrap
Text File
|
1988-11-07
|
11KB
|
275 lines
/* MaxApplZone.c */
/* ==================================================================== */
/* basic HyperCard memory management function */
/* ⌐ Copyright 1988 Sam Thornton, PO Box 123, Fairfield, NE 68938-0123 */
/* May be copied & distributed without charge only. Not for commercial */
/* sale or resale. */
/* Written in LSC 3.01 11/2/88 - some portions ⌐ by Symantec */
/* ==================================================================== */
/* SYNTAX: get MaxApplZone() */
/* Puts up a modeless dialog that displays current memory */
/* available and current time. Clicking on box next to */
/* MEMORY compacts the application heap. Clicking on the */
/* box next to the TIME display momentarily displays the */
/* current date (MM/DD/YY). Returns NULL. */
/* */
/* get MaxApplZone("C") */
/* Bypasses the modeless dialog, compacts the application */
/* heap and returns the number of KB free. */
/* */
/* get MaxApplZone ( < anything except 'C' > ) */
/* Bypasses modeless dialog and memory compaction to return */
/* the number of KB free. */
/* */
/* If all you need are the latter two functions, see the */
/* stripped down version in the accompanying file: */
/* <MaxApplZone.simple.c> */
/* */
/* NOTE that, due to some unavoidable heap fragmentation, the number */
/* of KB free returned or displayed is normally a little bit more */
/* than is actually usable. */
/* */
/* XCMD glue is based on the file LSCXCM.SIT/binary, modified slightly */
/* to partially dereference stringpointers...makes for slightly less */
/* distracting way to do some of the callback routines, e.g.: */
/* */
/* the proto function: */
/* pascal void SendCardMessage(paramPtr, msg) */
/* register XCmdBlockPtr paramPtr; Ptr msg; (vs. StringPtr msg;) */
/* ----------------------------- */
/* ...allows: SendCardMessage(paramPtr, "\pgo next card"); */
/* without any other contortions. */
/* */
/* In your LSC PROJECT file, make sure to add the following files: */
/* <MacTraps>, <XCMD glue file/library>, <unixst2d.c> */
/* and set the Project Type to: CODE/XFCN, ID nnn, NAME: MaxApplZone */
/* ==================================================================== */
#include "HyperXCmd.h"
#include "SetUpA4.h"
#include "MemoryMgr.h"
#include "WindowMgr.h"
#include "DialogMgr.h"
#include "EventMgr.h"
#include "MacTypes.h"
#include "QuickDraw.h"
#include "unix.h"
/* ===================================================================== */
/* SOME CONSTANTS...
/* ===================================================================== */
#define MAIN_DIALOG 297 /* RESOURCE ID FOR OUR DIALOG */
#define _DATE_TIME 2 /* CORRESPONDS TO BOX ITEMS IN OUR ITEM LIST */
#define _COMPACT 1
#define _QUIT 9 /* ARBITRARY VALUE FOR QUIT FLAG */
/* ===================================================================== */
/* ===================================================================== */
/* SET ASIDE SOME GLOBALS FOR LAZIER PARAMETER MANAGEMENT */
/* ===================================================================== */
static int dt_mode; /* FLAG: DISPLAY DATE OR TIME */
static long the_ticks; /* OUR GLOBAL COUNTDOWN TIMER */
static Rect myGlobalRect; /* H/C SCREEN DISPLAY AREA */
static XCmdBlockPtr myParamPtr; /* COPY OF HYPERCARD CALLBACK */
/* POINTER FOR FILTERPROC */
/* ===================================================================== */
/* ===================================================================== */
/* ===================================================================== */
/* ENTRYPOINT */
/* ===================================================================== */
/* ===================================================================== */
pascal void main (paramPtr)
XCmdBlockPtr paramPtr;
{
int itemHit, mem_size;
pascal Boolean MyFilter();
DialogPtr myDialogPtr;
GrafPtr current_port;
RememberA0();
SetUpA4();
/* ===================================================================== */
/* IF ANY PARAMETER IS RECEIVED, SKIP DIALOG & RETURN MEMSIZE */
/* ===================================================================== */
if (paramPtr->paramCount != 0) {
/* ...BUT IF THE PARAMETER STARTS WITH 'C', WE WANT TO COMPACT FIRST */
if (**(paramPtr->params[0]) == 'C' || **(paramPtr->params[0]) == 'c')
compact_it();
/* RETURN AVAILABLE MEMORY IN KB (plenty of room for expansions) */
paramPtr->returnValue = NewHandle(10);
if (paramPtr->returnValue == 0L) /* ... just to make sure */
SysBeep(1);
else {
mem_size = (TheZone->zcbFree) / 1024;
stci_d(*(paramPtr->returnValue), mem_size, 9);
}
} /* FALL THRU TO EXIT...*/
/* ===================================================================== */
/* ...OTHERWISE, PUT UP OUR DIALOG BOX AND RUN IT */
/* ===================================================================== */
else if (paramPtr->paramCount == 0) {
myDialogPtr = GetNewDialog(MAIN_DIALOG, 0, -1L);
/* CHECK TO MAKE SURE THERE WAS ENOUGH MEMORY */
if (myDialogPtr == 0L) { /* oops! -- better bail out */
SysBeep(10);
RestoreA4();
return;
}
/* MAKE HYPERCARD CALLBACK ADDRESS AVAILABLE TO MODALDIALOG FILTER */
myParamPtr = paramPtr;
/* SET DATE/TIME FLAG FOR DISPLAY OF CURRENT TIME: 0=time, 1=date */
dt_mode = 0;
/* DESCRIBE THE AREA IN WHICH OUR DIALOG CAN BE MOVED */
/* NOTE: can't seem to use 'screenBits.bounds' from HyperCard, so... */
GetPort(¤t_port);
myGlobalRect = current_port->portRect;
myGlobalRect.top += 20;
InsetRect (&myGlobalRect, 4, 4);
InitCursor (); /* SET CURSOR TO ARROW */
show_mem(myDialogPtr); /* setup dialog (see below) */
ShowWindow(myDialogPtr); /* dialog is initially invisible */
/* LOOP THRU MODALDIALOG UNTIL CLOSEBOX IS CLICKED */
do {
ModalDialog(MyFilter, &itemHit);
} while (itemHit != _QUIT);
DisposDialog(myDialogPtr);
}
/* ===================================================================== */
/* EXIT BACK TO HYPERCARD */
/* ===================================================================== */
RestoreA4();
return;
}
/* ===================================================================== */
/* ===================================================================== */
/* SUBROUTINES */
/* ===================================================================== */
/* ===================================================================== */
/* ===================================================================== */
/* MODALDIALOG FILTERPROC HANDLES OUR MEAGER REQUIREMENTS */
/* ===================================================================== */
pascal static Boolean MyFilter(dptr, theEvent, itemHit)
DialogPtr dptr; EventRecord *theEvent; int *itemHit;
{
int theHit; /* LOCAL SPARE */
if (Ticks >= the_ticks) show_mem(dptr); /* IF TIMER ELAPSED, UPDATE */
/* IF NO MOUSEHIT, LET MODALDIALOG HANDLE THIS CYCLE */
if (theEvent->what != mouseDown) return (false);
/* FIND OUT WHERE THE MOUSE WAS CLICKED AND HANDLE IT */
/* NOTE: Since this is strictly a ModalDialog subroutine, we don't
have to worry about hits in other parts of the screen */
switch(FindWindow(theEvent->where, &dptr)) {
case inDrag: /* DRAG DIALOG AROUND - NOTE: callback routine
modified in XCMD.c to dereference msg param */
DragWindow(dptr, theEvent->where, &myGlobalRect);
/* lazy update of HC screen */
SendCardMessage(myParamPtr, "\pgo this card");
return(true);
case inGoAway: /* SET ITEMHIT TO OUR QUIT FLAG, IF APPROPRIATE */
if (TrackGoAway(dptr, theEvent->where)) *itemHit = _QUIT;
return(true);
case inContent: /* RESPOND TO HITS IN OUR DIALOG CONTENT REGION */
if (DialogSelect(theEvent, &dptr, &theHit)) {
switch(theHit) {
case _COMPACT:
comp_Mem(dptr);
return(true);
case _DATE_TIME:
dt_mode = 1;
show_mem(dptr);
return(true);
} /* END OF 'IN CONTENT' SWITCH */
} /* END OF 'IF DIALOGSELECT' */
} /* END OF FINDWINDOW SWITCH */
return(false); /* nothing interesting happened--let's bore ModalDialog */
} /* END OF FUNCTION */
/* ===================================================================== */
/* COMPACT THE APPLICATION HEAP AND JSR TO DISPLAY ROUTINE */
/* ===================================================================== */
comp_Mem(whichWindow)
DialogPtr whichWindow;
{
compact_it();
show_mem(whichWindow);
return;
}
/* ===================================================================== */
/* COMPACT THE APPLICATION HEAP */
/* ===================================================================== */
compact_it()
{
Size grow;
MaxMem(&grow);
MaxApplZone();
return;
}
/* ===================================================================== */
/* FUNCTION TO DISPLAY CURRENT MEMSIZE & TIME (OR DATE) IN OUR DIALOG */
/* ===================================================================== */
show_mem(whichWindow)
DialogPtr whichWindow;
{
char mem_string[11];
char time_string[11];
NumToString(((TheZone->zcbFree) / 1024),&mem_string); /* AVAIL. MEM */
if (dt_mode == 0) /* WE EITHER WANT THE CURRENT TIME, OR...*/
IUTimeString(Time, 1, &time_string); /* '1' = HH:MM:SS format */
else if (dt_mode == 1) { /* ...WE WANT THE CURRENT DATE */
IUDateString(Time, 0, &time_string); /* '0' = MM/DD/YY format */
dt_mode = 0;
}
/* THIS IS PURTY KLUNKY - PROLLY NEED TO DRAWR IT DIREKLY */
ParamText(mem_string,time_string,"","");
DrawDialog(whichWindow);
the_ticks = Ticks + 55; /* RESET OUR TIMER */
return;
}
/* ===================================================================== */